﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Xml;

using System.Net;

using System.Data;

public partial class test34 : System.Web.UI.Page
{
    XmlDocument doc = new XmlDocument();

    int i;

    DataSet ds;

    int j;

 
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btn_Click(object sender, EventArgs e)
    {
        
//retrieve ip address

        string host = System.Net.Dns.GetHostName();

        string hostname = Dns.GetHostEntry(host).HostName;

        IPHostEntry ipEntry = Dns.GetHostEntry(host);

        IPAddress[] addr = ipEntry.AddressList;

 

        //xml

        doc.Load(Server.MapPath("~/IpCount.xml"));

        XmlNodeList list = doc.SelectNodes("//NewDataset/section");

        foreach (XmlNode node in list)

        {

            //checking whether the ip address is there or not

            if (addr[0].ToString() == node.ChildNodes[1].InnerText)

            {

                i = Convert.ToInt32(node.ChildNodes[2].InnerText);

                i += 1;

                node.ChildNodes[2].InnerText = i.ToString();

                doc.Save(Server.MapPath("~/IpCount.xml"));

                j = 1;

            }

            else

            {

                j = 0;

            }

        }

 

        if (j == 0)

        {

            ds = new DataSet();

            ds.ReadXml(Server.MapPath("~/IpCount.xml"));

            DataRow row = ds.Tables[0].NewRow();

            row["Count"] = "1";

            row["IPAddress"] = addr[0].ToString();

            row["Host"]=hostname.ToString();

            ds.Tables[0].Rows.Add(row);

            ds.WriteXml(Server.MapPath("~/IpCount.xml"));

            Response.Write(" no of clicks for this image from ur ip address " + i + 1);

        }

        else

            Response.Write(" no of clicks for this image from ur ip address " + i);

    
 
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        DataSet authorsDataSet;
        string filePath = Server.MapPath("IpCount.xml");
        authorsDataSet = new DataSet();
        //Read the contents of the XML file into the DataSet
        authorsDataSet.ReadXml(filePath);
        GridView1.DataSource = authorsDataSet.Tables[0].DefaultView;
        GridView1.DataBind();
    }
}
